﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Enterprise.Model.Role;
using Enterprise.Service.Role;
using Enterprise.Infrastructure;
using Enterprise.Control;
using Enterprise.Model.User;
using Enterprise.Service.User;
namespace Enterprise.UI.Web.Manage.Role
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                OnStart();                
            }
        }

        private void OnStart()
        {
            RoleService ser = new RoleService();
            var q = ser.RoleList();
            GridView1.DataSource = q;
            GridView1.DataBind();

            CreateBT.AddButton("Create.aspx", Utility.PopedomType.New, HeadMenuWebControls1);
        }

        protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onMouseOver"] = "js.ItemOver(this)";
            }
        }

        protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            RoleService ser = new RoleService();
            int index = Convert.ToInt32(e.CommandArgument); //获取行号
            int did = (int)GridView1.DataKeys[index].Value;
            var q = ser.RoleDisp(did);
            if (q != null)
            {
                switch (e.CommandName)
                {
                    case "mDelete":
                        q.DB_Option_Action = "Delete";
                        ser.RoleExecute(q);
                        //删除角色权限
                        UserRoleService urService = new UserRoleService();
                        urService.UserRoleExecuteBySql("Delete from SYS_USERROLE where ROLEID=" + did);
                        //删除用户、角色对应关系
                        RolePermissionService rpService = new RolePermissionService();
                        rpService.RoleExecuteBySql("Delete from SYS_ROLEPERMISSION where ROLEID=" + did);
                        break;
                }

                OnStart();
            }
        }

    }
}